// 知识点：很多算法在默认情况下都会使用元素类型的<或者==运算符完成比较，但标准库还为其定义了额外的版本，允许我们提供自己定义的操作代替默认的运算符，也就是说，向算法传递函数
#include <algorithm>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>

using namespace std;

void elimDups(vector<string> &s) {
  cout << "排序前：";
  for (int i = 0; i < s.size(); ++i) {
    cout << s[i] << " ";
  }
  cout << endl << "sort()排序后：";
  sort(s.begin(), s.end()); // sort排序
  for (int i = 0; i < s.size(); ++i) {
    cout << s[i] << " ";
  }
  cout << endl << "unique()排序后：";
  vector<string>::iterator str = unique(s.begin(), s.end()); // unique排序
  for (int i = 0; i < s.size(); ++i) {
    cout << s[i] << " ";
  }
  cout << endl << "erase()操作后：";
  s.erase(str, s.end()); // erase()操作
  for (int i = 0; i < s.size(); ++i) {
    cout << s[i] << " ";
  }
}

bool isShorter(const string &s1, const string &s2) {
  return s1.size() < s2.size(); // s1的大小是否小于s2
}
int main(int argc, char **argv) {
  string a[10] = {"because", "I",    "Like", "Like",   "C++",
                  "very",    "very", "much", "that's", "why"};
  vector<string> s(a, a + 10);
  elimDups(s);
  stable_sort(s.begin(), s.end(),
              isShorter); //将自定义的函数传递进来,先长度、后字典序
  cout << endl << "stable_sort排序后：";
  for (int i = 0; i < s.size(); ++i) {
    cout << s[i] << " ";
  }

  return 0;
}